package prefix.transaction;

import java.math.BigDecimal;

import models.Transaction;
import models.Transaction.Type;
import prefix.common.SystemActor;
import prefix.exception.TransactionException;
import prefix.exception.TransactionException.CODE;
import prefix.integration.wm.AbstractWalletManager.ExecutionStatus;

public class PayFeeFactory extends TransactionFactory {

    abstract class PayFeeTransaction extends BasicTransaction {
        public PayFeeTransaction() {
            // TODO Auto-generated constructor stub
        };

        @Override
        Integer executeCreateTransaction() throws TransactionException {
            throw new TransactionException(CODE.NO_NEED_CREATION);
        }

        @Override
        ExecutionStatus executeRollbackTransaction() throws TransactionException {
            // TODO Auto-generated method stub
            throw new TransactionException(CODE.NO_NEED_FAIL);
        }

        @Override
        ExecutionStatus executeTransaction() throws TransactionException {
            // TODO Auto-generated method stub
            try {
                return manager.pushFundsToBank(this.dbTrans.getFromAid(), this.dbTrans.getToAid(), new BigDecimal(
                        dbTrans.getAmount()));
            } catch (Exception e) {
                throw new TransactionException(CODE.ERROR_IN_WM, e);
            }
        }
    }

    public class T4_BORROWER_PAY_ORIG_FEE extends PayFeeTransaction {

        public T4_BORROWER_PAY_ORIG_FEE() {
            // TODO Auto-generated constructor stub
        }

        public T4_BORROWER_PAY_ORIG_FEE(Transaction trans) {
            this.dbTrans = trans;
        }

        @Override
        Type[] getAcceptedTypes() {
            return new Type[] { Type.T4_BORROWER_PAY_ORIG_FEE };
        }

        @Override
        void validateAmount() throws TransactionException {
            BigDecimal balance = manager.getBalance(this.dbTrans.getFromAid());
            if (balance.doubleValue() - this.dbTrans.getAmount() < 0)
                throw new TransactionException(CODE.NOT_ENOUGH_MONEY);
        }

        @Override
        void validateWallet() throws TransactionException {
            if (!SystemActor.ORIGINATION_FEE_ACCOUNT_W2.getId().equals(this.dbTrans.getToAid()))
                throw new TransactionException(CODE.INVALID_WALLET);
        }
    }

    public class T5_BORROWER_PAY_MANAGEMENT_FEE extends PayFeeTransaction {

        public T5_BORROWER_PAY_MANAGEMENT_FEE() {
            // TODO Auto-generated constructor stub
        }

        public T5_BORROWER_PAY_MANAGEMENT_FEE(Transaction trans) {
            this.dbTrans = trans;
        }

        @Override
        Type[] getAcceptedTypes() {
            return new Type[] { Type.T5_BORROWER_PAY_MANAGEMENT_FEE };
        }

        @Override
        void validateAmount() throws TransactionException {
            BigDecimal balance = manager.getBalance(this.dbTrans.getFromAid());
            if (balance.doubleValue() - this.dbTrans.getAmount() < 0)
                throw new TransactionException(CODE.NOT_ENOUGH_MONEY);
        }

        @Override
        void validateWallet() throws TransactionException {
            if (!SystemActor.BORROWER_MGMT_FEE_W3.getId().equals(this.dbTrans.getToAid()))
                throw new TransactionException(CODE.INVALID_WALLET);
        }
    }

    public class T6_LENDER_PAY_SERVICE_FEE extends PayFeeTransaction {

        public T6_LENDER_PAY_SERVICE_FEE() {
            // TODO Auto-generated constructor stub
        }

        public T6_LENDER_PAY_SERVICE_FEE(Transaction trans) {
            this.dbTrans = trans;
        }

        @Override
        Type[] getAcceptedTypes() {
            return new Type[] { Type.T6_LENDER_PAY_SERVICE_FEE };
        }

        @Override
        void validateAmount() throws TransactionException {
            BigDecimal balance = manager.getBalance(this.dbTrans.getFromAid());
            if (balance.doubleValue() - this.dbTrans.getAmount() < 0)
                throw new TransactionException(CODE.NOT_ENOUGH_MONEY);
        }

        @Override
        void validateWallet() throws TransactionException {
            if (!SystemActor.SERVICE_FEE_ACCOUNT_W4.getId().equals(this.dbTrans.getToAid()))
                throw new TransactionException(CODE.INVALID_WALLET);
        }
    }

}
